﻿using ApiDoc.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls;

namespace ApiDoc.Admin.View.Api
{
    /// <summary>
    /// frmInterface.xaml 的交互逻辑
    /// </summary>
    public partial class frmInterface : BasePage
    {
        private HttpClientApi httpClient;
      
        public frmInterface()
        {
            InitializeComponent();
        }
         
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            this.httpClient = new HttpClientApi();
           
            LoadAllFolder();
        }
         
        #region 文件夹

        private void tvFolder_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem treeViewItem = (TreeViewItem)this.tvFolder.SelectedItem;
            if (treeViewItem != null && treeViewItem.Items.Count == 0)
            {
                this.LoadFolderData();
            }
        }

        private async void LoadAllFolder()
        {
            List<FolderModel> folders = await this.httpClient.FolderListAll();

            this.tvFolder.Items.Clear();

            AddChildNode(0, folders, this.tvFolder.Items);
        }

        private async void LoadFolderData()
        {
            int parentSN = 0;
            ItemCollection Items = this.tvFolder.Items;
            if (this.tvFolder.SelectedItem != null)
            {
                TreeViewItem tviRoot = (TreeViewItem)this.tvFolder.SelectedItem;
                parentSN = ((FolderModel)tviRoot.DataContext).SN;
                Items = tviRoot.Items;
            }

            List<FolderModel> folders = await this.httpClient.FolderList(parentSN);

            Items.Clear();
            foreach (FolderModel folder in folders)
            {
                AddTreeNode(folder);
            }
        }

        private void AddTreeNode(FolderModel folder)
        {
            ItemCollection Items = this.tvFolder.Items;
            if (this.tvFolder.SelectedItem != null)
            {
                TreeViewItem tviRoot = (TreeViewItem)this.tvFolder.SelectedItem;
                Items = tviRoot.Items;
            }

            WQDTreeViewItem treeViewItem = CreateTreeViewItem(folder);
            Items.Add(treeViewItem);
        }
 
        private void AddChildNode(int ParentSN, List<FolderModel> folders, ItemCollection Items)
        {
            foreach (FolderModel folder in folders.FindAll(oo => oo.ParentSN == ParentSN))
            {
                WQDTreeViewItem tviChild = CreateTreeViewItem(folder);
                Items.Add(tviChild);

                AddChildNode(folder.SN, folders, tviChild.Items);
            }
        }
         
        private WQDTreeViewItem CreateTreeViewItem(FolderModel folder)
        {
            WQDTreeViewItem treeViewItem = new WQDTreeViewItem();
            treeViewItem.DataContext = folder;
            treeViewItem.Header = folder.FolderName;
            return treeViewItem;
        }

        #endregion

        private void btnFindInterface_Click(object sender, RoutedEventArgs e)
        { 
            this.LoadData();
        }

        private void btnApiAdd_Click(object sender, RoutedEventArgs e)
        {
            doType = PageDoType.Add;
            if (this.tvFolder.SelectedItem == null)
            {
                MessageBox.Show("请选择目录");
                return;
            }
            FolderModel folderModel = (FolderModel)this.tvFolder.SelectData;
            ApiModel model = new ApiModel();
            model.FKSN = 0;
            if (folderModel != null)
            {
                model.FKSN = folderModel.SN;
            }

            base.Contain.Navigate("/View/Api/frmInterfaceEdit", model);
        }

        private void btnApiEdit_Click(object sender, RoutedEventArgs e)
        {
            doType = PageDoType.Update;

            if (this.dgv.SelectedValue == null)
            {
                MessageBox.Show("请选择数据");
                return;
            }
 
            ApiModel model = (ApiModel)this.dgv.SelectedValue; 
            base.Contain.Navigate("/View/Api/frmInterfaceEdit", model);
        }

        private void btnApiDelete_Click(object sender, RoutedEventArgs e)
        {
           
        }

        public override void CallBack(object model)
        {
            base.CallBack(model);

            switch (doType)
            {
                case PageDoType.Add:
                    this.LoadData();
                    break;
                case PageDoType.Update:
                    this.LoadData(); 
                    break;
            }

        }
         
        private async void LoadData()
        {
            string id = "";
            if (this.tvFolder.SelectedItem != null)
            {
                TreeViewItem tvi = (TreeViewItem)this.tvFolder.SelectedItem;
                id = ((FolderModel)tvi.DataContext).Id;
            }

            string title = this.txtDgvSearch.Text;
            this.dgv.ItemsSource = await this.httpClient.InterfaceList(title, id);
        }
    }
}
